﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.MODEL.FoundationModel;
using MINERP.DAL.NewPhotoRindGrid;
using MINERP.MODEL.NewPhotoRindGrid;
using MINERP.COM;

namespace MINERP.BLL.NewPhotoRindGrid
{
    /// <summary>
    /// 处理分片中与照片相关的业务逻辑
    /// </summary>
    public class PhotoBLL_Grid
    {
        /// <summary>
        /// 根据orderProductId得到产品相应的照片信息
        /// </summary>
        /// <param name="orderProductId"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public List<PhotoInfo_Info> GetPhotosByOrderProductId(int orderProductId, out int result)
        {
            result = 0;//成功

            List<PhotoInfo_Info> ResultList = new List<PhotoInfo_Info>();
            PhotoDAL_Grid _photoDal = new PhotoDAL_Grid();

            if (orderProductId > 0)
            {
                ResultList = _photoDal.GetPhotoInfoByOrderProductId(orderProductId, out result);
                if (ResultList==null||ResultList==null)
                {
                    result = 3;
                }
            }
            else
            {
                result = 1;
            }
            return ResultList;

        }


        /// <summary>
        /// 检查输入的条码照片是否为返单的照片
        /// </summary>
        /// <param name="strBarcode"></param>
        /// <param name="OrderProductId"></param>
        /// <param name="photoId"></param>
        /// <returns>3:返单;2:正常单; -1:barCode不合法;-2:从数据库取数据出错;0:没有读到数据</returns>
        public  int CheckIsReBackOrder(string strBarcode, out int OrderProductId, out int photoId,out int parentProductId)
        {
            int result = 1;//成功
            OrderProductId = 0;
            photoId = 0;
            parentProductId = 0;

            if (strBarcode.StartsWith("3")&&strBarcode.Length==10)
            {
                PhotoDAL_Grid _photoDal = new PhotoDAL_Grid();
                result = _photoDal.GetPhotoInfo_InfoByBarCode(strBarcode, out photoId, out OrderProductId, out parentProductId);
                if (result==1)//成功
                {
                    if (GetIdFromBarCode(strBarcode)==photoId)
                    {
                        result = 2;//正常单
                    }
                    else
                    {
                        result = 3;//返单
                    }
                }
            }
            else
            {
                result = -1;//参数不合法
            }

            return result;
        }

        /// <summary>
        /// 更新照片的状态
        /// </summary>
        /// <param name="photoId"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public int UpdatePhotoStatus(int photoId, PhotoStatus status)
        {
            int result = 1;

            if (photoId>0)
            {
                PhotoDAL_Grid _photoDal = new PhotoDAL_Grid();

               result= _photoDal.UpPhotoStatus(photoId, (int)status);
            }
            else
            {
                result = -1;
            }
            return result;
        }

        /// <summary>
        ///  更新照片的条码等于2000+photoid,并更新主产品相应照片和本照片的状态为“分片完成”
        /// </summary>
        /// <param name="PhotoId"></param>
        /// <returns></returns>
        public int UpdatePhotoBarCode(int PhotoId)
        {
            int result = 1;
            string strBarCode;
            if (PhotoId>0)
            {
                PhotoDAL_Grid _photoDal = new PhotoDAL_Grid();
                strBarCode = "2" + String.Format("{0,9:d}", PhotoId);
                result=_photoDal.UpdateBarCode(PhotoId, strBarCode);

            }
            else
            {
                result = -1;
            }
            return result;
        }      


        #region help methods
       
        private int GetIdFromBarCode(string barCode)
        {
            return Convert.ToInt32(barCode.Substring(1));
        }
        #endregion
    }
}